<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

$defaultName = APPLICATION_NAME."_".util::now()."_".$CURRENT_USER->userid.".tgz";
$defaultContent = "application/gzip";

if ($param['op']=="up") {
	$filename = UPLOAD_PATH."/".$param['file'].".tgz";
} else if ($param['op']=="si") {
	$filename = TEMP_PATH."/".$param['file'];	 	
	$defaultName = $param['name']; 
	$defaultContent = $param['type'];
} else {
	if (isset($param['cmd'])) {
		$filename = str_replace("#ROOT",ROOT_BROWSER_PATH,$param['path']);
		$fileinfo = pathinfo($filename);
		if ($param['cmd']=="downloadz") {
			$defaultName = $fileinfo['filename'].".tgz";
			$newName = TEMP_PATH."/".$defaultName;
			$tar = new Archive_Tar($newName,"gz");
			$tar->createModify($filename,"",$fileinfo['dirname']);	
			unset($tar);
			$filename=$newName;
		} else {
			$defaultName = $fileinfo['basename'];
			$defaultContent = "application/octet-stream";
		}
	} else {
		$filename = TEMP_PATH."/".$param['file'];	
	}
}

$_log->debug($filename);
# Make sure the file exists before sending headers
#-------------------------------------------------
if(!$fdl=fopen($filename,"rb")){
	$_log->error("Cannot open ".$filename);
   die("Sorry :-( . Cannot Open File!");
} else {
	$_log->debug("Sending ".$filename);
	# Send the headers then send the file
	#------------------------------------
	header("Cache-Control: ");# leave blank to avoid IE errors
	header("Pragma: ");# leave blank to avoid IE errors
	header("Content-type: $defaultContent");
	header("Content-Disposition: attachment; filename=\"".$defaultName."\"");
	header("Content-length:".(string)(filesize($filename)));
	sleep(1);
	fpassthru($fdl);
}
	
?>